﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using PublicTransport.Properties;
using System.IO;
using System.Runtime.CompilerServices;


namespace PublicTransport.DAL
{
    /// <summary>
    /// Trieda, ktorá poskytuje pripojenie k databáze (Singletom). Stará sa o celý životný
    /// cyklus pripojenia k databáze.
    /// </summary>
    class DBHelper
    {
        private static SQLiteConnection connection;
        private static String databaseFile = "database.db";

        public static readonly String STATION_NAME = "name";
        public static readonly String STATION_ID = "id";

        /// <summary>
        /// Singleton. Vytvára spojenie na databázu, ak pripojenie existuje tak vráti už
        /// existujúce spojenie. Prístup k metóde je synchronizovaný. 
        /// Databázu hľadá v koreňovom adresári SD karty s názvom database.db
        /// </summary>
        /// <returns>Otvorené pripojenie k databáze SQLite</returns>
        [MethodImpl(MethodImplOptions.Synchronized)]
        public static SQLiteConnection GetConnection()
        {
            lock (typeof(SQLiteConnection))
            {
                if (connection != null)
                    return connection;

                bool emptyDatabase = false;
                DirectoryInfo rootDir = new DirectoryInfo(@"\");
                foreach (DirectoryInfo di in rootDir.GetDirectories())
                {
                    //if directory and temporary
                    if ((di.Attributes == (FileAttributes.Directory | FileAttributes.Temporary)))
                    {
                        //add to collection of storage cards
                        databaseFile = di.Name + "\\" + databaseFile;
                        break;
                    }
                }

                if (connection == null || (connection.State != System.Data.ConnectionState.Open
                    || connection.State != System.Data.ConnectionState.Connecting))
                {
                    connection = new SQLiteConnection("Data Source=" + databaseFile + ";");
                    connection.Open();

                    //enable foreign key support
                    SQLiteCommand pragmaFkCommand = new SQLiteCommand(connection);
                    pragmaFkCommand.CommandText = "PRAGMA foreign_keys;";
                    pragmaFkCommand.ExecuteNonQuery();
                }
                return connection;
            }
     
        }

        /// <summary>
        /// Spojenie je uzatvorené s deštrukturom danej triedy. Ponecháva sa teda otvorené
        /// počas celej existencie aplikácie.
        /// </summary>
        ~DBHelper()
        {
            try
            {
                if (connection != null && connection.State != System.Data.ConnectionState.Closed)
                {
                    connection.Close();
                }
            } catch {};

        }
    }
}
